/**
 * 
 */
package demo.tellurium.module

import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.telluriumsource.dsl.DslContext;
import org.telluriumsource.ui.object.Repeat;
import org.testng.v6.Lists;

import demo.tellurium.framework.BaseUIModule;

/**
 * @author roger.xu
 *
 */
public class DoubanModule extends BaseUIModule {

    private static final String MODULE_NAME = "Douban";
    public static final String PATH_KEY = "cqp.answer.path";

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    public void defineUi() {
        ui.Container(uid: "Douban") {
            Container(uid: "PhotoAlbum", clocator: [tag: "div", id: "photo_album"]) {
                Container(uid: "All", clocator: [tag: "span", class: "pl"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
            }
            Container(uid: "PhotoList", clocator: [tag: "div", class: "photolstin"]) {
                Repeat(uid: "PhotoWrap", clocator: [tag: "div", class: "photo_wrap"]) {
                    Container(uid: "Photo", clocator: [tag: "a", direct: "true"]) {
                        Image(uid: "Thumb", clocator: [tag: "img"])
                    }
                    List(uid: "Links", clocator: [tag: "div", class: "pl"], separator: "a") {
                        UrlLink(uid: "{1} as User") // a:eq(0)
                        UrlLink(uid: "{2} as Comments") // a:eq(1)
                    }
                }
            }
            Container(uid: "Paginator", clocator: [tag: "div", class: "paginator"]) {
                Container(uid: "Next", clocator: [tag: "span", class: "next"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
            }
        }
    }

    public void goMainPage() {
        def linkUid = String.format("%s.PhotoAlbum.All.Link", getModuleName());
        String photoAlbumUrl = getAttribute(linkUid, "href");
        this.photoAlbumUrl = photoAlbumUrl;

        click linkUid
        pause 500
        waitForPageToLoad 30000
        pause 500
    }

    public boolean isLastPage() {
        def linkUid = String.format("%s.Paginator.Next.Link", getModuleName());
        def locator = getLocator(linkUid);
        int foundCount = getLocatorCount(locator);

        return foundCount == 0;
    }

    public String photoAlbumUrl = null;
    public int pageSize = 30;

    public void doNextPage() {
        def linkUid = String.format("%s.Paginator.Next.Link", getModuleName());
        click linkUid
        pause 500
        waitForPageToLoad 30000
        pause 500
    }

    private List<String> results = new ArrayList<String>();

    public void getResults() {
        String str = StringUtils.join(results, System.getProperty("line.separator"));
        console.debug(">>results:", str);
    }

    public void findUser(String name, Map<String, Object> settings) {
        boolean isFindAll = settings.get("isFindAll");

        def listUid = String.format("%s.PhotoList.PhotoWrap", getModuleName());
        int listSize = getRepeatNum(listUid);
        console.debug(listSize);
        getResults();

        boolean found = false;
        for (int i = 1; i <= listSize; i++) {
            String userUid = String.format("%s[%d].Links.User", listUid, i);
            String username = getText(userUid);

            if (StringUtils.equals(username, name)) {
                found = true;

                // user link
                String userLink = getLink(userUid);
                //console.debug(username, userLink);

                // photo link
                String photoUid = String.format("%s[%d].Photo", listUid, i);
                //String photoTitle = getAttribute(photoUid, "title");
                String photoLink = getAttribute(photoUid, "href");
                String thumbUid = String.format("%s[%d].Photo.Thumb", listUid, i);
                String thumbLink = getAttribute(thumbUid, "src");

                console.debug(username, photoLink);
                //this.results.add(username);
                this.results.add(thumbLink);
                this.results.add(photoLink);
            }
        }

        boolean isContinue = (found && isFindAll) || (!found);
        if (isContinue && !isLastPage()) {
            doNextPage();
            findUser(name, settings);
        }
    }
}
